<?php

if (!defined('IN_IA')) {
	exit('Access Denied');
}

class Member_EweiShopV2Page extends PluginWebPage
{
	public function main() {
		global $_W;
		global $_GPC;

		$groups = $this->model->getGroups();
		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;
		$params = array(':uniacid' => $_W['uniacid']);
		$condition = '';
		$keyword = trim($_GPC['keyword']);

		if (!empty($keyword)) {
			$condition .= ' and ( user_name like :keyword or shop_user_grade like :keyword or distribution_grade like :keyword)';
			$params[':keyword'] = '%' . $keyword . '%';
		}

		$status = empty($_GPC['status']) ? '' : intval($_GPC['status']);
		switch ($status) {
			case '1'://已审核，使用中
				$condition .= ' and status = 1 ';
				break;
			case '2'://待审核
				$condition .= ' and status = 0 ';
				break;
			case '3'://已过期
				$condition .= ' and ( status = 2 OR end_time < ' . time() . ' )';
				break;
			case '4'://已停用
				$condition .= ' and status = 3 ';
				break;
			default:
				$condition .= '';
				break;
		}

		$sql = 'select * from ' . tablename('ewei_shop_identity_member') . ' where uniacid=:uniacid ' . $condition . ' ORDER BY id ASC';

		if (empty($_GPC['export'])) {
			$sql .= ' limit ' . (($pindex - 1) * $psize) . ',' . $psize;
		}

		$list = pdo_fetchall($sql, $params);
		$total = pdo_fetchcolumn('select count(*) from' . tablename('ewei_shop_identity_member') . ' where uniacid = :uniacid ' . $condition, $params);
		if(!empty($list)){
			foreach ($list as $key => $value) {
		
				$member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uid' => $value['shop_user_id'], ':uniacid' => $_W['uniacid']));
				$merch = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_merch_user') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':openid' => $member['openid'], ':uniacid' => $_W['uniacid']));
				$mendian = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_info') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $value['shop_user_id']));

				$list[$key]['avatar'] = $member['avatar'];

				if($value['end_time'] < time() && !empty($value['end_time'])){
					// $list[$key]['status'] = '0';
				}
				$list[$key]['end_time'] = date('Y-m-d', $value['end_time']);


				// 会员
				$list[$key]['shop_user_grade'] = $this->model->level($member['level'], 'ewei_shop_member_level');
				// 分销等级
				$list[$key]['distribution_grade'] = $this->model->level($member['agentlevel'], 'ewei_shop_commission_level');
				// 门店
				$list[$key]['mendian_level'] = $this->model->level($mendian['mendian_level'], 'ewei_shop_mendian_level');
				// 多商户
				$list[$key]['merch_level'] = $this->model->level($merch['groupid'], 'ewei_shop_merch_group');
				// 区域代理等级
				$list[$key]['abonus_level'] = $this->model->level($member['aagentlevel'], 'ewei_shop_abonus_level');
				// 股东
				$list[$key]['globonus_level'] = $this->model->level($member['partnerlevel'], 'ewei_shop_globonus_level');
				
				if(empty($list[$key]['shop_user_grade'])){
					$list[$key]['shop_user_grade'] = '默认等级';
				}
				if(empty($list[$key]['distribution_grade'])){
					$list[$key]['distribution_grade'] = '默认等级';
				}


			}
		}
		


		$pager = pagination2($total, $pindex, $psize);
		load()->func('tpl');
		include $this->template();
	}

	public function edit()
	{
		$this->post();
	}

	protected function post()
	{
		global $_W;
		global $_GPC;

		$id = intval($_GPC['id']);
		$area_set = m('util')->get_area_config_set();
		$new_area = intval($area_set['new_area']);

		$item = pdo_fetch('select * from ' . tablename('ewei_shop_identity_member') . ' where id=:id and uniacid=:uniacid limit 1', array(':id' => $id, ':uniacid' => $_W['uniacid']));

		// 是否已有身份
		if(empty($item['identity_id'])){
			$identity = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		}else{
			$identity = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $item['identity_id']));
		}

		// 用户信息
		$member = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_member') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uniacid' => $_W['uniacid'], ':uid' => $item['shop_user_id']));
		
		// 购买的身份商品
		$goods = pdo_fetch('SELECT id,title FROM ' . tablename('ewei_shop_goods') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $item['buy_goods_id']));

		// 多用户信息
		$merch_user = pdo_fetch('SELECT groupid,status FROM ' . tablename('ewei_shop_merch_user') . ' WHERE uniacid = :uniacid AND openid = :openid', array(':openid' => $member['openid'], ':uniacid' => $_W['uniacid']));

		// 门店信息
		$mendian_info = pdo_fetch('SELECT mendian_level,status FROM ' . tablename('ewei_shop_mendian_info') . ' WHERE uniacid = :uniacid AND uid = :uid', array(':uid' => $member['uid'], ':uniacid' => $_W['uniacid']));
		

		// 会员等级
		$member_level = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_member_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($member_level as $key => $value) {
			$user[$value['id']] = $value;
		}

		// 分销等级
		$commission = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_commission_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($commission as $key => $value) {
			$distribution[$value['id']] = $value;
		}
		
		// 门店等级
		$mendian = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_mendian_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($mendian as $key => $value) {
			$mendian_level[$value['id']] = $value;
		}

		// 多商户分组
		$merch = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_merch_group') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($merch as $key => $value) {
			$merch_level[$value['id']] = $value;
		}

		// 全民股东等级
		$globonus = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_globonus_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($globonus as $key => $value) {
			$globonus_level[$value['id']] = $value;
		}

		// 区域代理等级
		$abonus = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_abonus_level') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		foreach ($abonus as $key => $value) {
			$abonus_level[$value['id']] = $value;
		}
		
		
		if ($_W['ispost']) {

			$identity = pdo_fetch('SELECT name FROM ' . tablename('ewei_shop_identity') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $_GPC['identity_id']));
			$shop_user_grade_id 	   = intval($_GPC['shop_user_grade_id']);
			$shop_user_grade 		   = $user[$shop_user_grade_id]['levelname'];//会员等级
			$end_shop_user_grade_id    = intval($_GPC['end_shop_user_grade_id']);
			$end_shop_user_grade 	   = $user[intval($_GPC['end_shop_user_grade_id'])]['levelname'];//到期会员等级
			$distribution_grade_id 	   = intval($_GPC['distribution_grade_id']);
			$distribution_grade 	   = $distribution[$distribution_grade_id]['levelname'];//分销等级
			$end_distribution_grade_id = intval($_GPC['end_distribution_grade_id']);
			$end_distribution_grade    = $distribution[$end_distribution_grade_id]['levelname'];//到期分销等级
			$start_time                = strtotime($_GPC['start_time']);
			$end_time				   = strtotime($_GPC['end_time']);

			
			// 商品信息
			$goods_id = intval($_GPC['buy_goods_id']);
			$res = pdo_fetch('SELECT id,title,productprice,marketprice FROM ' . tablename('ewei_shop_goods') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $goods_id));
			

			$data = array(
				'uniacid' 					=> $_W['uniacid'],
				'identity_id'				=> $_GPC['identity_id'],
				'identity'					=> $identity['name'],
				'is_use_member_level'		=> $_GPC['is_use_member_level'],
				'shop_user_grade_id' 		=> $shop_user_grade_id,
				'shop_user_grade'    		=> $shop_user_grade,
				'end_shop_user_grade_id'	=> $end_shop_user_grade_id,
				'end_shop_user_grade'   	=> $end_shop_user_grade,
				'is_use_distribution_level' => $_GPC['is_use_distribution_level'],
				'distribution_grade_id' 	=> $distribution_grade_id,
				'distribution_grade'		=> $distribution_grade,
				'end_distribution_grade_id' => $end_distribution_grade_id,
				'end_distribution_grade'	=> $end_distribution_grade,
				'buy_goods_id'				=> $goods_id,
				'buy_goods_title'			=> $res['title'],
				'buy_goods_price'			=> $res['marketprice'],
				'is_use_merch'				=> $_GPC['is_use_merch'],
				'merch_id'					=> $_GPC['merch_id'],
				'end_merch_id'				=> $_GPC['end_merch_id'],
				'is_use_abonus_agent'		=> $_GPC['is_use_abonus_agent'],
				'abonus_agent_id'			=> $_GPC['abonus_agent_id'],
				'end_abonus_agent_id'		=> $_GPC['end_abonus_agent_id'],
				'is_use_globonus_partner'	=> $_GPC['is_use_globonus_partner'],
				'globonus_partner_id'		=> $_GPC['globonus_partner_id'],
				'end_globonus_partner_id'	=> $_GPC['end_globonus_partner_id'],
				'is_use_mendian'			=> $_GPC['is_use_mendian'],
				'mendian_level_id'			=> $_GPC['mendian_level_id'],
				'end_mendian_level_id'		=> $_GPC['end_mendian_level_id'],
				'start_time'				=> $start_time,
				'end_time'					=> $end_time,
				'status'					=> intval($_GPC['status']),
			);
			if($end_time < time()){
				$dara['status'] = 2;
			}

			
			if (empty($_GPC['id'])) {
				$data['add_time'] = time();
				pdo_insert('ewei_shop_identity_member', $data);
				$id = pdo_insertid();
			}
			else {
				$data['edit_time'] = time();
				pdo_update('ewei_shop_identity_member', $data, array('id' => $id));
				
			}

			if($_GPC['status'] > 0){
				// 用户表修改 ewei_shop_member
				$mdata = array(
					'level' 		=> $shop_user_grade_id, 
					'isagent' 		=> $_GPC['is_use_distribution_level'], //是否分销商
					'agentlevel' 	=> ($_GPC['status'] == 3) ? $end_distribution_grade_id : $distribution_grade_id, 
					'ispartner' 	=> $_GPC['is_use_globonus_partner'], //是否股东
					'partnerlevel' 	=> ($_GPC['status'] == 3) ? $_GPC['end_globonus_partner_id'] : $_GPC['globonus_partner_id'], 
					'isaagent' 		=> $_GPC['is_use_abonus_agent'], //是否区域代理
					'aagentlevel' 	=> ($_GPC['status'] == 3) ? $_GPC['end_abonus_agent_id'] : $_GPC['abonus_agent_id'],
				);
				pdo_update('ewei_shop_member', $mdata, array('uniacid' => $_W['uniacid'], 'uid' => $item['uid']));
				// 多商户用户表 ewei_shop_merch_user
				if( $_GPC['is_use_merch'] == 1){
					$mudata = array(
						'uniacid'=> $_W['uniacid'],
						'openid' => $member['openid'],
						'groupid' => ($_GPC['status'] == 3) ? $_GPC['end_merch_id'] : $_GPC['merch_id'],
						'status' => 0,
					);
					if(empty($merch_user)){
						pdo_insert('ewei_shop_merch_user', $mudata);
					}else{
						pdo_update('ewei_shop_merch_user', $mudata, array('uniacid' => $_W['uniacid'], 'openid' => $member['openid']));
					}
				}
				// 门店用户表 ewei_shop_mendian_info
				if( $_GPC['is_use_mendian'] == 1){
					$mddata = array(
						'uniacid'=> $_W['uniacid'],
						// 'openid' => $member['openid'],
						'mendian_level' => ($_GPC['status'] == 3) ? $_GPC['end_mendian_level_id'] : $_GPC['mendian_level_id'],
						'start_time' => $start_time,
						'end_time' => $end_time,
					);
					if(empty($mendian_info)){
						pdo_insert('ewei_shop_mendian_info', $mddata);
					}else{
						pdo_update('ewei_shop_mendian_info', $mddata, array('uniacid' => $_W['uniacid'], 'uid' => $member['uid']));
					}
				}
			}

			if($_GPC['status'] == 1 && $item['status'] != '1'){
				$ploginfo = array('uniacid' => $_W['uniacid'], 'uid' => $member['uid'], 'add_time' => time(), 'end_time' => $end_time, 'identity_id' => $_GPC['identity_id'], 'identity_level' => $identity['name'], 'old_identity' => $item['identity'], 'old_identity_id' => $item['identity_id'], 'content' => '用户' . $member['nickname'] . '的身份'. $identity['name'] . '启用', 'status' => 1);
				pdo_insert('ewei_shop_identity_plog', $ploginfo);

			} elseif ($_GPC['status'] == 2 && $item['status'] != '2') {
				$ploginfo = array('uniacid' => $_W['uniacid'], 'uid' => $member['uid'], 'add_time' => time(), 'end_time' => $end_time, 'identity_id' => $_GPC['identity_id'], 'identity_level' => $identity['name'], 'old_identity' => $item['identity'], 'old_identity_id' => $item['identity_id'], 'content' => '用户' . $member['nickname'] . '的身份'. $identity['name'] . '停用', 'status' => 2);
				pdo_insert('ewei_shop_identity_plog', $ploginfo);

			} elseif ($_GPC['status'] == 3 && $item['status'] != '3') { 
				$ploginfo = array('uniacid' => $_W['uniacid'], 'uid' => $member['uid'], 'add_time' => time(), 'end_time' => '', 'identity_id' => '', 'identity_level' => '', 'old_identity' => $identity['name'], 'old_identity_id' => $_GPC['identity_id'], 'content' => '用户' . $member['nickname'] . '的身份'. $identity['name'] . '已恢复', 'status' => 3);
				pdo_insert('ewei_shop_identity_plog', $ploginfo);
			}
			
			show_json(1, array('url' => webUrl('identity/member', array('status' => $data['status']))));
		}

		
		
		include $this->template();
	}

	public function status()
	{
		global $_W;
		global $_GPC;
		$id = intval($_GPC['id']);

		if (empty($id)) {
			$id = (is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0);
		}

		$items = pdo_fetchall('SELECT id,user_name FROM ' . tablename('ewei_shop_identity_member') . ' WHERE id in( ' . $id . ' ) AND uniacid=' . $_W['uniacid']);

		foreach ($items as $item) {
			pdo_update('ewei_shop_identity_member', array('status' => intval($_GPC['status'])), array('id' => $item['id']));
			plog('identity.category.edit', ('修改会员身份状态<br/>ID: ' . $item['id'] . '<br/>会员名称: ' . $item['user_name'] . '<br/>状态: ' . $_GPC['status']) == 1 ? '显示' : '隐藏');
		}

		show_json(1, array('url' => referer()));
	}

	public function delete()
	{
		global $_W;
		global $_GPC;
		$id = intval($_GPC['id']);

		if (empty($id)) {
			$id = (is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0);
		}

		$uniacid = $_W['uniacid'];
		$items = pdo_fetchall('SELECT * FROM ' . tablename('ewei_shop_identity_member') . ' WHERE id in( ' . $id . ' ) AND uniacid=' . $_W['uniacid']);

		foreach ($items as $item) {
			pdo_delete('ewei_shop_identity_member', array('id' => $item['id'], 'uniacid' => $uniacid));
			plog('identity.member.delete', '删除`会员身份 <br/>身份:  ID: ' . $item['id'] . ' / 名称:   ' . $item['name'] . '<br/>会员等级： ' . $item['shop_user_grade'] . '<br/>分销等级： ' . $item['distribution_grade']);
		}

		show_json(1);
	}
}